package com.skplanet.oneplusonewindow.moimserver.controller.api;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skplanet.oneplusonewindow.moimserver.domain.Moim_Plaza;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_PlazaBoard;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_PlazaComment;
import com.skplanet.oneplusonewindow.moimserver.service.PlazaService;


@Controller
public class PlazaAPIController {
	
	@Autowired
	private PlazaService plazaService;

	@ResponseBody
	@RequestMapping( value = { "/json/plaza/getBoard", "/json/plaza/getBoard.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> getBoard(
			@RequestParam(value="boardId", required=true)long boardId) {

		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		mapJSON.put("boars", plazaService.getPlazaBoard(boardId));
		return mapJSON;
	}

	@ResponseBody
	@RequestMapping( value = { "/json/plaza/getPlaza", "/json/plaza/getPlaza.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> getPlaza(
			@RequestParam(value="plazaId", required=true)long plazaId) {

		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		mapJSON.put("boars", plazaService.getPlaza(plazaId));
		return mapJSON;
	}
	@ResponseBody	
	@RequestMapping( value = { "/json/plaza/List", "/json/plaza/List.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> plazaList(
			@RequestParam(value="moimId", required=true)long moimId) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		mapJSON.put("boars", plazaService.getPlazaList(moimId));
		return mapJSON;
	} 
	
	@ResponseBody	
	@RequestMapping( value = { "/json/plaza/boardList", "/json/plaza/boardList.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> boardList(
			@RequestParam(value="plazaId", required=true)long plazaId) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		mapJSON.put("boars", plazaService.getBoardList(plazaId));
		return mapJSON;
	} 

	@ResponseBody
	@RequestMapping( value = { "/json/plaza/writePlaza", "/json/plaza/writePlaza.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> writePlaza(
			@RequestParam(value="moimId", required=true)long moimId,
			@RequestParam(value="title", required=true)String title,
			@RequestParam(value="content", required=true)String content
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Plaza plaza = new Moim_Plaza();
		plaza.setTitle(title);
		plaza.setContent(content);
		plazaService.addPlaza(moimId, plaza);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}	
	
	@ResponseBody
	@RequestMapping( value = { "/json/plaza/writeBoard", "/json/plaza/writeBoard.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> writeBoard(
			@RequestParam(value="plazaId", required=true)long plazaId,
			@RequestParam(value="title", required=true)String title,
			@RequestParam(value="content", required=true)String content,
			@RequestParam(value="userId", required=true)long userId
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_PlazaBoard plazaboard = new Moim_PlazaBoard();
		plazaboard.setTitle(title);
		plazaboard.setContent(content);
		plazaboard.setUserId(userId);
		plazaService.addBoard(plazaId, plazaboard);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}	
	
	@ResponseBody
	@RequestMapping( value = { "/json/plaza/updateBoard", "/json/plaza/updateBoard.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> updateBoard(
			@RequestParam(value="boardId", required=true)long boardId,
			@RequestParam(value="title", required=true)String title,
			@RequestParam(value="content", required=true)String content
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_PlazaBoard plazaboard = new Moim_PlazaBoard();
		plazaboard.setTitle(title);
		plazaboard.setContent(content);
		plazaService.updateBoard(boardId, plazaboard);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}	
	@ResponseBody
	@RequestMapping( value = { "/json/plaza/updatePlaza", "/json/plaza/updatePlaza.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> updatePlaza(
			@RequestParam(value="plazaId", required=true)long plazaId,
			@RequestParam(value="title", required=true)String title,
			@RequestParam(value="content", required=true)String content
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Plaza plaza = new Moim_Plaza();
		plaza.setTitle(title);
		plaza.setContent(content);
		plazaService.updatePlaza(plazaId, plaza);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}	
	
	@ResponseBody
	@RequestMapping( value = { "/json/plaza/deleteBoard", "/json/plaza/deleteBoard.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> deleteBoard(
			@RequestParam(value="boardId", required=true)long boardId
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_PlazaBoard board = plazaService.getPlazaBoard(boardId);
		plazaService.deleteBoard(board);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}
	@ResponseBody
	@RequestMapping( value = { "/json/plaza/deletePlaza", "/json/plaza/deletePlaza.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> deletePlaza(
			@RequestParam(value="plazaId", required=true)long plazaId
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Plaza plaza = plazaService.getPlaza(plazaId);
		plazaService.deletePlaza(plaza);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}
	@ResponseBody	
	@RequestMapping( value = { "/json/plaza/commentList", "/json/plaza/commentList.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> commentList(
			@RequestParam(value="boardId", required=true)long id) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		mapJSON.put("boars", plazaService.getCommentList(id));
		return mapJSON;
	} 
	@ResponseBody
	@RequestMapping( value = { "/json/plaza/writeComment", "/json/plaza/writeComment.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> writeComment(
			@RequestParam(value="boardId", required=true)long boardId,
			@RequestParam(value="userId", required=true)long userId,
			@RequestParam(value="content", required=true)String content
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_PlazaComment comment = new Moim_PlazaComment();
		comment.setUserId(userId);
		comment.setContent(content);
		plazaService.addComment(boardId, comment);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}	
	
	@ResponseBody
	@RequestMapping( value = { "/json/plaza/updateComment", "/json/plaza/updateComment.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> updateComment(
			@RequestParam(value="commentId", required=true)long commentId,
			@RequestParam(value="content", required=true)String content
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_PlazaComment comment = new Moim_PlazaComment();
		comment.setContent(content);
		plazaService.updateComment(commentId, comment);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}	
	
	@ResponseBody
	@RequestMapping( value = { "/json/plaza/deleteComment", "/json/plaza/deleteComment.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> deleteComment(
			@RequestParam(value="commentId", required=true)long commentId
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_PlazaComment comment = plazaService.getComment(commentId);
		
		plazaService.deleteComment(comment);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}
}
